/*                                    _
 *   ___ _ __ ___ _ __ ___   ___   __| |
 *  / __| '__/ __| '_ ` _ \ / _ \ / _` |
 *  \__ \ |  \__ \ | | | | | (_) | (_| |
 *  |___/_|  |___/_| |_| |_|\___/ \__,_|                                  
 *
 * |_  _ | _  _  _ _   _ _  _  _|   | _ 
 * | |(/_||_)(/_| _\  | | |(_)(_||_||(/_
 *        |                             
 */

#define MODULE_NAME "Helpers"
#define SURVIVORINDEX_REBUILD_DELAY 0.3

#include "srs.helpers\enums.sp"
#include "srs.helpers\constants.sp"
#include "srs.helpers\macros.sp"
#include "srs.helpers\debug.sp"


stock CountInGameHumans()
{
	new realPlayers = 0;
	FOR_EACH_HUMAN(i)
	{
		realPlayers++;
	}
	return realPlayers;
}

//client is in-game and not a bot
stock bool:IsClientInGameHuman(client)
{
	return IsClientInGame(client) && !IsFakeClient(client);
}

stock RemoveAllEntitiesByName(const String:sItemName[])
{
	new ent = -1;
	new prev = 0;
	while ((ent = FindEntityByClassname(ent, sItemName)) != -1)
	{
		if (prev)
		{
			RemoveEdict(prev);
			Debug_Print("Removed %d \"%s\"", prev, sItemName);
		}
		else
		{
			Debug_Print("---Removing \"%s\"---", sItemName);
		}
		prev = ent;
	}
	if (prev)
	{
		RemoveEdict(prev);
		Debug_Print("Removed %d \"%s\"", prev, sItemName);
	}
}

stock bool:IsVersus()
{
	decl String:gamemode[56];
	GetConVarString(FindConVar("mp_gamemode"), gamemode, sizeof(gamemode));
	
	if (StrContains(gamemode, "versus", false) > -1) return true;
	return false;
}

stock CheatCommand(client, String:command[], String:arguments[]="") // works with KAC if root cheat is enabled
{
	if (!client || !IsClientInGame(client))
	{
		for (new target = 1; target <= MaxClients; target++)
		{
			if (IsClientInGame(target))
				client = target;
		}
	}
	
	new userflags = GetUserFlagBits(client);
	SetUserFlagBits(client, ADMFLAG_ROOT);
	new flags = GetCommandFlags(command);
	SetCommandFlags(command, flags & ~FCVAR_CHEAT);
	FakeClientCommand(client, "%s %s", command, arguments)
	SetCommandFlags(command, flags);
	SetUserFlagBits(client, userflags);
}

stock GetEntityAbsOrigin(entity, Float:origin[3])
{
	if (entity > 0 && IsValidEntity(entity))
	{
		decl Float:mins[3], Float:maxs[3];
		GetEntPropVector(entity,Prop_Send,"m_vecOrigin",origin);
		GetEntPropVector(entity,Prop_Send,"m_vecMins",mins);
		GetEntPropVector(entity,Prop_Send,"m_vecMaxs",maxs);
		
		origin[0] += (mins[0] + maxs[0]) * 0.5;
		origin[1] += (mins[1] + maxs[1]) * 0.5;
		origin[2] += (mins[2] + maxs[2]) * 0.5;
	}
}

stock RequireL4D2()
{
	decl String:gameName[64];
	GetGameFolderName(gameName, sizeof(gameName));
	if (!StrEqual(gameName, "left4dead2", /*sensitive*/false))
	{
		SetFailState("Plugin supports Left 4 Dead 2 only.");
	}
}

#undef MODULE_NAME
